package com.ruoyi.consumer.user.service.impl;


import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.CaptchaException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.consumer.user.domain.User;

import com.ruoyi.consumer.user.domain.vo.UserLogin;
import com.ruoyi.consumer.user.mapper.UserMapper;
import com.ruoyi.consumer.user.service.LoginService;
import com.ruoyi.system.api.RemoteUserService;

import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName: LoginServiceImpl
 * @description: $
 * @author: GCC
 * @date: 2025/2/25 15:35
 * @Version: 1.0
 */

@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private  UserMapper userMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private RemoteUserService remoteUserService;
    @Override
    public Map userLogin(UserLogin userLogin) {



        String code= userLogin.getCode();
        String mobile=userLogin.getPhone();
        //验证码 验证
        //获取登录数据

        if (StringUtils.isEmpty(code))
        {
            throw new CaptchaException("验证码不能为空");
        }
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + mobile;
        String captcha = redisService.getCacheObject(verifyKey);

        if (captcha == null)
        {
            throw new CaptchaException("验证码已失效");
        }
//            redisService.deleteObject(verifyKey);
        if (!code.equalsIgnoreCase(captcha))
        {
            throw new CaptchaException("验证码错误");
        }


        //验证成功  清除缓存  查询验证表 通过手机号 获得商家信息
        //清除缓存
        redisService.deleteObject(verifyKey);
//
//        //验证通过
//
//        //通过手机号 获取 用户信息
//
        User user = new User();

        user.setPhone(userLogin.getPhone());

        List<User> users = userMapper.selectUserList(user);
        //获取 用户信息

        System.out.println("users = " + users);
        //数据为空


        //不存在用户 插入数据

        if (Objects.isNull(users)  || users.size()==0){
            userMapper.insertUser(user);
        }
        else {    //存在用户  获取数据

            user=users.get(0);
        }


//        SysUser sysUser = new SysUser();
//
//
//        sysUser.setUserId(3L);
//        sysUser.setUserName("用户");
//
//        userLogin.setSysUser(sysUser);
//
//        userLogin.setUser(user);
        String userName="user";
//
        //写死 获取 用户权限
        R<LoginUser> userResult = remoteUserService.getUserInfo(userName, SecurityConstants.INNER);

//        LoginShop2 loginShop = new LoginShop2();
//
//        loginShop.setSysUser(userResult.getData().getSysUser());
//        loginShop.setRoles(userResult.getData().getRoles());
//        loginShop.setPermissions(userResult.getData().getPermissions());

//        UserLogin loginShop = new UserLogin();
//
//        loginShop.setSysUser(userResult.getData().getSysUser());
//        loginShop.setRoles(userResult.getData().getRoles());
//        loginShop.setPermissions(userResult.getData().getPermissions());
//        System.out.println("userResult.getData() = " + userResult.getData());




//        System.out.println("userLogin = " + userLogin);
//        System.out.println("(LoginUser)userLogin = " + (LoginUser)userLogin);
     ;

//        LoginUser loginUser = new LoginUser();
        LoginUser loginUser=userResult.getData();
//        userLogin1.setSysUser(userResult.getData().getSysUser());
//        userLogin1.setRoles(userResult.getData().getRoles());
//        userLogin1.setPermissions(userResult.getData().getPermissions());

//        loginUser.setSysUser(userResult.getData().getSysUser());
//        loginUser.setRoles(userResult.getData().getRoles());
//        loginUser.setPermissions(userResult.getData().getPermissions());

        Map<String, Object> token = tokenService.createToken(loginUser);

        token.put("user",user);


        //生成token

        return token;
    }

}
